package com.example.crm_system.controller;

import com.example.crm_system.config.Result;
import com.example.crm_system.entity.Params;
import com.example.crm_system.entity.User;
import com.example.crm_system.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {

    // 测试使用 ---> 打印日志到控制台
    private Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource()
    private UserService userService;

    // 插入user
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        if (user.getEno() == null) {
            userService.insert(user);
        } else {
            userService.update(user);
        }
        return Result.success();
    }

    // 删除user
    @DeleteMapping("/delete/{eno}")
    public Result delete(@PathVariable Integer eno) {
        userService.delete(eno);
        return Result.success();
    }

    // 查询方法
    @GetMapping("/searchUser")
    public Result searchCustomer(Params params) {
        PageInfo<User> list = userService.searchUser(params);
        return Result.success(list);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User loginUser = userService.login(user);
        return Result.success(loginUser);
    }

}
